package zkk.com.mengqu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGABannerUtil;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.FoodType;

/**
 * Created by Administrator on 2017/7/16.
 */
public class StewardFragment extends Fragment implements View.OnClickListener,BGABanner.Delegate<ImageView, String>{
    private Context context;
    private View view;
    private ViewPager pager;
    private TextView recommend,news;//为你推荐、消息中心
    private BGABanner banner;
    private LinearLayout foodType;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_steward, container,false);
        context = getContext();
        initView();
        setBanner();
        setImage(context, foodType);
        return view;
    }

    private void initView(){
        banner = (BGABanner) view.findViewById(R.id.banner);
        foodType = (LinearLayout) view.findViewById(R.id.ll_food_type);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_kitchen_share:
                setActionBarTabSelected(0);
                break;
            case R.id.tx_private_menu:
                setActionBarTabSelected(1);
                break;
            default:
                break;
        }
    }

    //设置banner
    public void setBanner(){
        List<View> views = new ArrayList<>();
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.drawable.ic_banner , ImageView.ScaleType.CENTER_INSIDE));
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.drawable.ic_banner2, ImageView.ScaleType.CENTER_INSIDE));
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.drawable.ic_banner3, ImageView.ScaleType.CENTER_INSIDE));
        views.add(BGABannerUtil.getItemImageView(getActivity(), R.drawable.ic_banner4, ImageView.ScaleType.CENTER_INSIDE));
        banner.setData(views);//设置数据源
        banner.setDelegate(this);//设置点击监听
    }

    //banner点击监听
    @Override
    public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
        Toast.makeText(getActivity(), "点击了第" + (position + 1) + "页", Toast.LENGTH_SHORT).show();
    }

    private void setActionBarTabSelected(int paramInt){
        pager.setCurrentItem(paramInt);
        if(paramInt == 0){
            recommend.setSelected(true);
            news.setSelected(false);

            recommend.setTextColor(getResources().getColor(R.color.black));
            news.setTextColor(getResources().getColor(R.color.tab_menu_text));
        }else if(paramInt==1){
            news.setSelected(true);
            recommend.setSelected(false);

            news.setTextColor(getResources().getColor(R.color.black));
            recommend.setTextColor(getResources().getColor(R.color.tab_menu_text));
        }
    }

    public void setImage(final Context context, final LinearLayout layout) {


        APIManager.setManager()
                .getAllType()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<FoodType>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(final List<FoodType> foodTypes) {
                        layout.removeAllViews();
                        for (int i = 0; i < foodTypes.size(); i++) {
                            final int position = i;
                            View view = LayoutInflater.from(context).inflate(R.layout.item_food_type,null);
                            ImageView imageView = (ImageView) view.findViewById(R.id.iv_type_icon);
                            TextView textView = (TextView) view.findViewById(R.id.tx_type_value);
                            textView.setText(foodTypes.get(i).getValue());
                            imageView.setPadding(30, 10, 0, 10);
                            view.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Toast.makeText(getActivity(), "" + foodTypes.get(position).getValue(), Toast.LENGTH_SHORT).show();
                                }
                            });
                            layout.addView(view);
                        }
                    }
                });


    }
}